<?php
/**
 * The base Config object.
 */
class Config {
    /**
     * Configuration paramters.
     * @var array
     */
	protected $config = array();
    /**
     * Is this object read only?
     * @var bool
     */
	protected $readonly;

    /**
     * Class constructor.
     */
	public function __construct() {
	    $this->config['general']['page'] = DEFAULT_PAGE;
	    $this->config['general']['method'] = DEFAULT_METHOD;
        $this->config['general']['databaseconfigsection'] = DEFAULT_DATABASECONFIGSECTION;
	    $this->config['general']['page404'] = DEFAULT_PAGE404;
	    $this->config['general']['method404'] = DEFAULT_METHOD404;
	    $this->config['general']['dateformat'] = DEFAULT_DATEFORMAT;
	    $this->config['general']['debug'] = DEFAULT_DEBUG;
        $this->readonly = false;
	}

    /**
     * Get the config parameters as an array.
     * @return array
     */
	public function getConfigAsArray() {
		return $this->config;
	}

    /**
     * Get a section from the config object as a Config_Section object.
     * @param string $section
     * @return Config_Section
     * @throws Config_Exception
     */
	public function getSection($section) {
        if(!array_key_exists($section, $this->config)) {
            throw new Config_Exception("Section '$section' does not exist.");
        }
		if(!is_array($this->config[$section])) {
            $this->config[$section] = array();
        }
        return new Config_Section($this->config[$section], $this->readonly);
	}

    /**
     * Set a value if we're not read only.
     * @param string $section
     * @param string $key
     * @param mixed $value
     * @throws Config_Exception
     */
	public function setConfig($section, $key, $value) {
		if($this->readonly) {
			throw new Config_Exception('Config is read only');
		}
		$this->config[$section][$key] = $value;
	}
}
?>
